#include"preCmpH.h"
#include "UIControl.h"
#include"UIControlContainer.h"
#include "UIManager.h"

BEGIN_NAMESPACE(ui_manager)
BEGIN_NAMESPACE(ui_controls)
//=====================================
// Constructors, Destructor, Operators
//=====================================

UIControl::UIControl() 
	: pos_()
	, size_()
	, visible_(true)
	, background_()
	, name_()
	, parent_(null)
	, focused_(false)
	, bound_(false)
	, focus_(null)
{}
UIControl::~UIControl()
{
	parent_ = null;
}
//=====================================
// Functionality
//=====================================

void UIControl::BoundBy( UIManager& manager, bool is_component )
{
	bound_ = true;
	mcInterface_.LoadManager(manager);
	if(!is_component && parent_ == null){
		mcInterface_.getRoot().AddChild(this);
	}
}
void UIControl::UnboundBy( UIManager& manager )
{
	bound_ = false;
	mcInterface_.UnloadManager();
}
void UIControl::Render(UIPoint parentPosistion)
{
}
// UNFINISHED
void UIControl::HandleEvent(ui_events::UIEvent ui_event)
{
	switch (ui_event.getMsgFlag())
	{
	case ui_events::UIEvent::FLAG_KEYBOARD:
		{
		}
		break;
	case ui_events::UIEvent::FLAG_MOUSE:
		{
		}
		break;
	case ui_events::UIEvent::FLAG_RENDER:
		{
			//Render();
		}
	default:
		break;
	}
}
// UNFINISHED
void UIControl::TestInvariant() const
{

}
//=====================================
// Setters
//=====================================

void UIControl::setFocus(UIControl& focus)
{
	if(focused_){
		this->focus_ = &focus;
		this->focused_ = false;
		focus.focused_ = true;
	} else {
		if(focus_ != null){
			focus_->setFocus(focus);
		} else {
			if(parent_ != null){
				parent_->setFocus(focus);
			}
		}
	}
}
void UIControl::setPosition( UIPoint pos ){pos_ = pos;}
void UIControl::setSize( UISize size ){size_ = size;}
void UIControl::setBackground( ui_manager::ui_graphics::UIImage* img ){background_ = img;}
void UIControl::setVisible( bool is_visible ){visible_ = is_visible;}
void UIControl::setName( std::string name ){ name_ =name;}
void UIControl::setParent( UIControlContainer& parent ){parent_ = &parent;}
//=====================================
// Getters
//=====================================

UIPoint UIControl::getPosition() const{return pos_;}
UISize UIControl::getSize() const{return size_;}
ui_manager::ui_graphics::UIImage& UIControl::getBackground() const{return *background_;}
bool UIControl::isVisible() const{return visible_;}
std::string UIControl::getName() const{return name_;}
bool UIControl::hasParent() const{return parent_ == null ? false : true;}
const UIControlContainer& UIControl::getParent() const{return *parent_;}
const UIControl& UIControl::getFocus(){return *focus_;}

END_NAMESPACE // UI controls
END_NAMESPACE // UI manager